<?php
class models_thongke extends connect{

	function tongsodong()
	{
		$sql="SELECT * from mathang";
		$this->query_db($sql);
		return $this->totalRow();

	}
	function danhsachmh($start,$limit){
		$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong, Gia, HinhAnh, MoTa, AnHien
					FROM mathang 
					LIMIT $start,$limit";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function tkhoadon($tunam,$dennam){
		$sql="SELECT MaHD,MaKH,NgayLap,TinhTrang
			 FROM hoadon
			WHERE DATE_FORMAT(NgayLap,'%Y') BETWEEN '$tunam' AND '$dennam'";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function listhdthang($ngay)
	{
		$sql="SELECT MaHD, MaKH, DATE_FORMAT(NgayLap,'%d/%m%/%Y') as NgayLap
			FROM hoadon
			WHERE DATE_FORMAT(NgayLap,'%m%/%Y')='$ngay'";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function listcthoadon($id)
	{
		$sql="SELECT  MaHD, MaMH, SoLuongBan, DonGiaBan
			FROM chitiethoadon
			WHERE MaHD ='$id'";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function listcthd()
	{
		$sql="SELECT  MaHD, MaMH, SoLuongBan, DonGiaBan
				FROM chitiethoadon";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function listmh(){
		$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong, Gia, HinhAnh, MoTa, AnHien
					FROM mathang";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function listctn($id){
		$sql = "SELECT MaCTN, MaNCC, MaMH, ROUND(AVG(DonGiaNhap)) as DonGiaNhap, SoLuongNhap, NgayNhap
					FROM chitietnhap
					WHERE MaMH ='$id'";
		$this->query_db($sql);
		return $this->fetchOne();
	}
	function listctnhap(){
		$sql = "SELECT MaCTN, MaNCC, MaMH, DonGiaNhap, SoLuongNhap, NgayNhap
					FROM chitietnhap
					";
		$this->query_db($sql);
		return $this->fetchAll();
	}
}
class pagertk{
	function findstart($limit)
	{
		if ((!isset($_GET['page'])) || ($_GET['page']=="1"))
		{
			$start=0;
			$_GET['page']=1;
		}
		else
		{
			$start=($_GET['page']-1)*$limit;
		}
		return $start;
	}
	function findpages($count,$limit)
	{
		$pages=(($count % $limit)==0) ? $count / $limit : floor($count / $limit);
		return $pages;
	}
	function pagelist($curpage , $pages)
	{
		$page_list="";
		 
		if(($curpage !=1) && ($curpage))
		{
			$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=1\" title=\"Trang đầu\"> << </a> ";
		}
		if(($curpage-1) > 0)
		{
			$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=".($curpage-1)."\" title=\"Trang trước\"> < </a>";
		}
		for ($i=1; $i<=$pages; $i++)
		{
			if($i==$curpage)
			{
				$page_list .="<b>".$i."</b>";
			}
			else
			{
				$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=".$i."\" title=\"Trang".$i."\">".$i."</a>";
			}
			$page_list .=" ";
		}
		if (($curpage+1) <=$pages)
		{
			$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=".($curpage+1)."\" title=\"Trang sau\"> > </a>";
		}
		if(($curpage !=$pages) && ($pages != 0)){

			$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=".$pages."\" title=\"Trang cuối\"> >> </a>";
		}

		$page_list .="</td>\n\n";
		return $page_list;
	}
	function nextprev($curpage,$pages)
	{
		$next_prev="";
		if(($curpage-1)<=0)
		{
			$next_prev .="Ve trang truoc";
		}
		else
		{
			$next_prev .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=".($curpage-1)."\"> Trang truoc</a>";
		}
		$next_prev .=" || ";
		if (($curpage+1) > $pages)
		{
			$next_prev .="Den trang sau";
		}
		else
		{
			$next_prev .="<a href=\"".$_SERVER['PHP_SELF']."?mod=tonkho"."&page=".($curpage+1)."\"> Trang sau</a>";
		}
		return $next_prev;
	}
}
